package com.aotain.oframework.util;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * @author Jacob.cw.Zhang@gmail.com
 */
public class DateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
	
	public static final String JSON_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

	public Date deserialize(JsonElement json, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
		DateFormat df = new SimpleDateFormat(JSON_DATE_FORMAT);
		if (!(json instanceof JsonPrimitive)) {
			throw new JsonParseException("The date should be a string value");
		}
		try {
			Date date = df.parse(json.getAsString());
			return date;
		} catch (ParseException e) {
			throw new JsonParseException(e);
		}
	}

	public JsonElement serialize(Date src, Type arg1, JsonSerializationContext arg2) {
		DateFormat df = new SimpleDateFormat(JSON_DATE_FORMAT);
		String dateFormatAsString = df.format(new Date(src.getTime()));
		return new JsonPrimitive(dateFormatAsString);
	}
}
